<?php
namespace Auditing\Controller;
use Auditing\Model\DoctorModel;
use Think\Controller;
use Think\Image;

class IndexController extends Controller {
    public function index(){
        echo '主页';
    }
    /**
     * 医生认证
     */
    public function certification(){
        $doctor_model = D('Doctor');
        if(IS_POST){
            if(!I('post.approve')){
                echo "<script>alert('请阅读并接收开店协议');history.back()</script>";
                exit;
            }
            if(empty($_FILES['img_idz']['tmp_name']) || empty($_FILES['img_idf']['tmp_name'])){
                echo "<script>alert('请上传你的身份证图片，或图片超过限定大小');history.back()</script>";
                exit;
            }
            //收集数据，验证，完成，提交
            if($doctor_model->create() === false){
                $this->error(get_errors($doctor_model));
            }
            if($doctor_model->addDoctor() === false){
                $this->error(get_errors($doctor_model));
            }
            //成功跳转到等待页面
            $this->redirect('certificationWait',0);
        }else{
            //获取当前用户信息
            $user_token = session('NOW_USER_TOKEN');
            //根据审核状态 来获取不同参数
            $doctor_model = new DoctorModel();
            $status = $doctor_model->getDoctorStatus($user_token);
            //判断是否被拒绝
            $res = $doctor_model->queryDoctorFail($user_token);
            if($res != null){
                //审核失败
                $this->assign('status','fail');
                $this->assign('content',$res['content']);
            }elseif ($status == '1' || $status == '0'){
                $this->redirect('Auditing/Index/certificationWait');
            }
            //获取大范围的地区
            $ares = M('areaassociation')->field('id_head,title')->where(['pid'=>0])->select();
            $this->assign('ares',$ares);
            $this->display();
        }
    }

    /**
     * 提交审核成功的等待页
     */
    public function certificationWait(){
        $user_token = session('NOW_USER_TOKEN');
        $user_info = M('user')->where(['token'=>$user_token])->find();
        //根据审核状态 来获取不同参数
        $doctor_model = new DoctorModel();
        $status = $doctor_model->getDoctorStatus($user_token);
        //判断是否已被拒绝
        $res = $doctor_model->queryDoctorFail($user_token);
        if($status == '0'){
            //审核中
            $res = 'wait';
        }elseif($status == '1'){
            //审核成功
            $res = 'success';
        }elseif($res != null){
            //认证失败的 跳到审核页面
            $this->redirect('Auditing/Index/certification');
        }else{
            $res = 'wait';
        }
        //获取当前用户信息
        $this->assign('status',$res);
        $this->assign($user_info);
        $this->display();
    }

    /**
     * 检测当前用户是否已经提交过审核了
     */
//    public function validateCouldExist(){
//        //从session中获取用户token
//        $user_token = session('NOW_USER_TOKEN');
//        $user = new UserModel();
//        $user_id = $user->getIdByToken($user_token);
//        $res = M('Doctor')->where(['user_id'=>$user_id])->find();
//        if($res){
//            $this->ajaxReturn(['status'=>'0']);
//        }else{
//            $this->ajaxReturn(['status'=>'1']);
//        }
//    }

    /**
     * 检测当前要加入的团队编号
     * @param $groupId
     */
    public function validateGroup($groupId){
        if(!IS_AJAX) E('页面不存在');
        $group = M('GroupTable')->field('name,label')->where(['label'=>$groupId])->find();
        if($group){
            $this->ajaxReturn(['name'=>$group['name'],'label'=>$group['label']]);
        }else{
            $this->ajaxReturn(['name'=>'','label'=>'']);
        }
    }

    /**
     * 检测当前手机号码是否为已注册用户
     * @param $phone
     */
    public function validatePhone($phone){
        if(!IS_AJAX) E('页面不存在');
        $res = M('user')->where(['phonenumber'=>$phone])->find();
        $this->ajaxReturn(['status'=>$res]);
    }

    /**
     * 管理我的医生资料
     */
    public function manageDoctor(){
        //获取该用户的医生资料
        $user_token = session('NOW_USER_TOKEN');//获取用户信息
        $user_info = M('userdetail')->where(['token'=>$user_token])->find();
        $doctor_info = M('Doctor')->where(['user_id'=>$user_info['userid']])->find();
        //将身份证信息加密
        $doctor_info['id_no'] = preg_replace('/(\d{2})\d{14}(\d{2})/','$1*************$2',$doctor_info['id_no']);
        $this->assign('doctor_info',$doctor_info);
        $this->display();
    }

    public function ajaxManageDoctor() {
        $group_model = D('Doctor');
        //收集数据 交于模型层处理
        $result = $group_model->editDoctor(I('post.'));
        //成功再次跳转到本页
        $this->ajaxReturn($result);
    }

    /**
     * ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
     * -------------------医生审核完成-----------------------------------------
     * ----------------------------------------------------------------------
     */

    /**
     * 创建医疗小组
     */
    public function createGroup(){
        $user_token = session('NOW_USER_TOKEN');//获取用户信息
        $user_info = M('userdetail')->where(['token'=>$user_token])->find();
        if(IS_POST){
            if(!I('post.approve')){
                echo "<script>alert('请阅读并接收开店协议');history.back()</script>";
                exit;
            }
            $group_model = D('GroupTable');
            //收集数据，验证，完成，提交
            if($group_model->create() === false){
                $this->error(get_errors($group_model));
            }
            if($group_model->addGroup() === false){
                $this->error(get_errors($group_model));
            }
            //成功跳转到管理页
            $this->redirect('manageGroup',0);
        }else{
            //判断当前用户是否已经创建过团队了
            $doctor = M('group_table')->where(['userid'=>$user_info['userid']])->find();
            if($doctor){
                echo "<script>alert('您已经创建了团队：".$doctor['name']."，不能再创建新团队。');history.back()</script>";
                exit;
            }
            //获取服务地区域
            $ares_c = M('areaassociation')->where(['flag'=>'C'])->field(['id,title'])->select();
            $ares_s = M('areaassociation')->where(['flag'=>'S'])->field(['id,title'])->select();
            $this->assign('ares_c',$ares_c);
            $this->assign('ares_s',$ares_s);
            $this->display();
        }
    }

    /**
     * 管理团队
     */
    public function manageGroup(){
        if(IS_POST){
            //判断是否为非法操作
            $user_token = session('NOW_USER_TOKEN');//获取用户信息
            $user_id = M('userdetail')->where(['token'=>$user_token])->getField('userid');
            $cli_id = M('group_table')->where(['userid'=>$user_id])->getField('id');
            if(I('post.id') != $cli_id){
                //非法篡改id 直接退出
                $this->redirect('User/Register/logout',0);
            }
            $group_model = D('GroupTable');
            //收集数据，验证，完成，提交
            if($group_model->create() === false){
                $this->error(get_errors($group_model));
            }
            if($group_model->editGroup(I('post.id')) === false){
                $this->error(get_errors($group_model));
            }
            //成功再次跳转到本页
            $this->redirect('manageGroup',0);
        }else{
            $user_token = session('NOW_USER_TOKEN');//获取用户信息
            $user_info = M('userdetail')->where(['token'=>$user_token])->find();
            $group_info = M('GroupTable')->where(['userid'=>$user_info['userid']])->find();
            //判断当前用户是否有创建小组
            if(!$group_info){
                echo '<script>alert("你还没有创建过小组");history.back()</script>';exit;
            }
            //获取小组服务地区
            $area_ids = M('group_serverare')->where(['groupid'=>$group_info['id']])->getField('are_id',true);
            //获取小组成员信息
            $user_ids = M('group_member')->where(['groupid'=>$group_info['id']])->getField('userid',true);
            $doctors = M('Doctor')->field('user_id,headurl,name,skill')->where(['user_id'=>['in',$user_ids],'status'=>1])->select();
            //获取服务地区域
            $ares_c = M('areaassociation')->where(['flag'=>'C'])->field(['id,title'])->select();
            $ares_s = M('areaassociation')->where(['flag'=>'S'])->field(['id,title'])->select();
            $this->assign('ares_c',$ares_c);
            $this->assign('ares_s',$ares_s);
            $this->assign('area_ids',$area_ids);
            $this->assign('doctors',$doctors);
            $this->assign('group_info',$group_info);
            $this->display();
        }
    }
    //ajax请求删除成员
    public function ajaxDeleteMember($user_id,$group_id){
        if(!IS_AJAX) E('页面不存在');
        //判断是否为非法操作
        $user_token = session('NOW_USER_TOKEN');//获取用户信息
        $userid = M('userdetail')->where(['token'=>$user_token])->getField('userid');
        $gro_id = M('GroupTable')->where(['userid'=>$userid])->getField('id');
        if($group_id != $gro_id){
            $this->ajaxReturn(['status'=>0,'msg'=>'非法操作！']);
        }
        //不能删除创建者
        if(M('GroupTable')->where(['userid'=>$user_id,'id'=>$group_id])->find()){
            $this->ajaxReturn(['status'=>0,'msg'=>'不能移除创建者']);
        }
        if(M('group_member')->where(['groupid'=>$group_id,'userid'=>$user_id])->delete()){
            $this->ajaxReturn(['status'=>1,'msg'=>'该成员已被移除本小组']);
        }else{
            $this->ajaxReturn(['status'=>0,'msg'=>'移除失败']);
        }
    }
    //ajax邀请成员加入
    public function ajaxInviteMember($phone,$flag,$group_id){
        if(!IS_AJAX) E('页面不存在');
        //判断是否为非法操作
        $user_token = session('NOW_USER_TOKEN');//获取用户信息
        $user_id = M('userdetail')->where(['token'=>$user_token])->getField('userid');
        $gro_id = M('GroupTable')->where(['userid'=>$user_id])->getField('id');
        if($group_id != $gro_id){
            $this->ajaxReturn(['status'=>0,'msg'=>'非法操作！']);
        }
        //先判断该成员是否在app中存在
        $user_info = M('user')->where(['phonenumber'=>$phone])->find();
        if (!$user_info){
            $this->ajaxReturn(['status'=>0,'msg'=>'该用户不存在，请前往官方APP注册账号，并提交审核资料']);
        }
        //再判断该成员是否提交过审核或审核状态
        $doctor_info = M('Doctor')->where(['user_id'=>$user_info['userid'],'status'=>1])->find();
        if (!$doctor_info){
            $this->ajaxReturn(['status'=>0,'msg'=>'该用户未通过审核或正在审核中']);
        }
        //当标识为B时即为确定添加成员
        if($flag == 'B'){
            //判断当前用户是否已经加入过该小组了
            $res = M('group_member')->where(['groupid'=>$group_id,'userid'=>$user_info['userid']])->find();
            if($res){
                $this->ajaxReturn(['status'=>0,'msg'=>'该用户已经加入过该小组了']);
            }
            //确定加入
            if(M('group_member')->add(['groupid'=>$group_id,'userid'=>$user_info['userid']])){
                $this->ajaxReturn(['status'=>2,'msg'=>'邀请成功']);
            }else{
                $this->ajaxReturn(['status'=>0,'msg'=>'邀请失败']);
            }
        }else{
            $this->ajaxReturn(['status'=>1,'name'=>$doctor_info['name']]);
        }
    }
    /**
     * 加入团队
     */
    public function joinGroup(){
        $user_token = session('NOW_USER_TOKEN');//获取用户信息
        $user_info = M('userdetail')->where(['token'=>$user_token])->find();
        //获取已加入的团队
        $group_ids = M('group_member')->where(['userid'=>$user_info['userid']])->getField('groupid',true);
        $group_ids = $group_ids ? $group_ids : '';
        $groups = M('group_table')->where(['id'=>['in',$group_ids]])->getField('id,name');
        $this->assign('groups',$groups);
        $this->display();
    }
    public function ajaxJoinGroup($group_label){
        if(!IS_AJAX) E('页面不存在');
        $user_token = session('NOW_USER_TOKEN');//获取用户信息
        $user_info = M('userdetail')->where(['token'=>$user_token])->find();
        $group_info = M('group_table')->where(['label'=>$group_label])->find();
        if($group_info){
            $group_member_model = M('group_member');
            //先判断该用户是否已经加入过该团队
            if($group_member_model->where(['groupid'=>$group_info['id'],'userid'=>$user_info['userid']])->find()){
                $this->ajaxReturn(['status'=>0,'msg'=>'您已经加入过该团队了，不能重复加入']);exit;
            }else{
                //将该用户加入小组成员表
                $res = $group_member_model->add(['groupid'=>$group_info['id'],'userid'=>$user_info['userid']]);
                if($res){
                    $this->ajaxReturn(['status'=>1,'msg'=>'加入成功']);exit;
                }else{
                    $this->ajaxReturn(['status'=>0,'msg'=>'加入失败']);exit;
                }
            }

        }else{
            $this->ajaxReturn(['status'=>0,'msg'=>'输入的编号有误，请确认后再输']);exit;
        }
    }
    //---------------------------------------------------------------------------
    //-----------------------------------诊所认证----------------------------------
    //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    /**
     * 诊所认证
     */
//    public function certificationClinic(){
//        if(IS_POST){
//            $clinic_model = D('ClinicTable');
//            if(!I('post.approve')){
//                echo "<script>alert('请阅读并接收开店协议');history.back()</script>";
//                exit;
//            }
//            if(empty($_FILES['commpy_img']['tmp_name']) || empty($_FILES['id_img_z']['tmp_name']) || empty($_FILES['id_img_f']['tmp_name'])){
//                echo "<script>alert('请上传你的身份证图片或营业执照');history.back()</script>";
//                exit;
//            }
//            //收集数据，验证，完成，提交
//            if($clinic_model->create() === false){
//                $this->error(get_errors($clinic_model));
//            }
//            if($clinic_model->addClinic() === false){
//                $this->error(get_errors($clinic_model));
//            }
//            //成功跳转到等待页面
//            $this->redirect('certificationClinicWait',0);
//        }else{
//            $user_token = session('NOW_USER_TOKEN');//获取用户信息
//            $user_info = M('userdetail')->where(['token'=>$user_token])->find();
//            //判断是否被拒绝
//            $res = M('ClinicFail')->where(['user_id'=>$user_info['userid']])->find();
//            $status = M('ClinicTable')->where(['userid'=>$user_info['userid']])->getField('status');
//            if($res != null){
//                //审核失败
//                $this->assign('status','fail');
//                $this->assign('content',$res['content']);
//            }elseif ($status == '1' || $status == '0'){
//                $this->redirect('Auditing/Index/certificationClinicWait');
//            }
//            //获取服务地区域
//            $ares = M('areaassociation')->field(['id,title'])->select();
//            $this->assign('ares',$ares);
//            $this->assign('user_info',$user_info);
//            $this->display();
//        }
//    }
//    /**
//     * 诊所提交审核成功的等待页
//     */
//    public function certificationClinicWait(){
//        $user_token = session('NOW_USER_TOKEN');
//        $user_info = M('user')->where(['token'=>$user_token])->find();
//        //根据审核状态 来获取不同参数
//        $status = M('ClinicTable')->where(['userid'=>$user_info['userid']])->getField('status');
//        //判断是否已被拒绝
//        $ress = M('ClinicFail')->where(['user_id'=>$user_info['userid']])->find();
//        if($status == '0'){
//            //审核中
//            $res = 'wait';
//        }elseif($status == '1'){
//            //审核成功
//            $res = 'success';
//        }elseif($ress != null){
//            //认证失败的 跳到审核页面
//            $this->redirect('Auditing/Index/certificationClinicWait');
//        }else{
//            $res = 'wait';
//        }
//        //获取当前用户信息
//        $this->assign('status',$res);
//        $this->assign($user_info);
//        $this->display();
//    }


    //异步头像上传
    public function ajaxHeadImg(){
        if($_FILES['headlog']['size'] > 2097152){
            echo json_encode(['status'=>0, 'msg'=>'上传失败，图片大小不能超过2M', 'url'=>''],JSON_UNESCAPED_UNICODE);exit;
        }
        $file = groupUpload();
        //返回处理的地址
        echo json_encode($file,JSON_UNESCAPED_UNICODE);
//        $this->ajaxReturn($file);
    }


}